import { defineStore } from "pinia";
import searchApi from '@/api/search'
import type { defaultKeysAndHotKeys, productData } from "@/api/interface/searchData";
interface searchSotreStore {
  hotData: defaultKeysAndHotKeys,
  searchList: string[],
  searchInfo: any
}
// category
interface category {
  id: number,
  name: string,
  frontName: null
}
type categoryListData = category[]
export const searchDataStore = defineStore('search', {
  state: (): searchSotreStore => {
    return {
      hotData: {
        defaultKeyword: {
          keyword: '',
          schemeUrl: '',
          highlight: 0,
          hidden: 0,
          type: 0,
          algSort: false,
          extra: {
            resource: {
              materialName: '',
              resourcesId: 0,
              materialId: 0
            }
          }
        },
        defaultKeywords: [],
        hotKeywordVOList: [],
        colorfulEggList: []
      },
      searchList: [],
      searchInfo: {}
    }
  },
  actions: {
    // 获取关键词和热门关键词
    async getKeywordAndHot() {
      try {
        const res = await searchApi.getKeyWords()
        this.hotData = res
      } catch (error) {
        console.log('失败' + error)
        console.log(error);
      }
    },
    async getSearchList(keyword: string) {
      try {
        const res = await searchApi.getMomentWords(keyword)
        this.searchList = res

      } catch (error) {
        console.log('失败' + error)
        console.log(error);
      }
    },
    async getSearchInfo(keyword: string) {
      try {
        const res = await searchApi.getSearchInfo(keyword)
        this.searchInfo = res
      } catch (error) {
        console.log('出现错误');
        console.log(error);
      }
    }
  },
  getters: {
    searchData(): productData[] {
      if (this.searchInfo.directlyList) return (this.searchInfo as any).directlyList
      else return []
    },
    categoryList(): categoryListData {
      if (this.searchInfo.categoryL1List) return (this.searchInfo as any).categoryL1List
      else return []
    }
  }
})